﻿using Dong.Model;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Stote.UControls
{
    public partial class UCStoreAreaBox : UserControl
    {
        public UCStoreAreaBox()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.Selectable, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.btnSetTemperature.Click += BtnSetTemperature_Click;
        }

        /// <summary>
        /// 控件的数据对象
        /// </summary>
        private StoreAreaBoxModel uBoxSource;
        public StoreAreaBoxModel UBoxSource
        {
            get { return uBoxSource; }
            set
            {
                uBoxSource = value;

                this.StoreAreaId = uBoxSource.StoreAreaId;
                this.StoreAreaNo = uBoxSource.StoreAreaNo;
                this.StoreAreaName = uBoxSource.StoreAreaName;
                this.Temperature = uBoxSource.Temperature;
                this.MinTemperature = uBoxSource.MinTemperature;
                this.MaxTemperature = uBoxSource.MaxTemperature;
                this.TemperatureRange = uBoxSource.TemperatureRange;
                this.TemperatureState = uBoxSource.TemperatureState;
                this.StateColor = uBoxSource.StateColor;
                this.BtnSetVisible = uBoxSource.BtnSetVisible;
                this.BtnSetText = uBoxSource.BtnSetText;
                this.ProductCount = uBoxSource.ProductCount;
            }
        }

        /// <summary>
        /// 仓库区域Id 
        /// </summary>
        public int StoreAreaId
        {
            get { return uBoxSource.StoreAreaId; }
            set
            {
                uBoxSource.StoreAreaId = value;
            }
        }

        /// <summary>
        /// 仓库区域编号
        /// </summary>
        public string StoreAreaNo
        {
            get
            {
                return uBoxSource.StoreAreaNo;
            }
            set
            {
                uBoxSource.StoreAreaNo = value;
            }
        }

        /// <summary>
        /// 仓库分区名称
        /// </summary>
        public string StoreAreaName
        {
            get { return uBoxSource.StoreAreaName; }
            set
            {
                uBoxSource.StoreAreaName = value;
                lblStoreAreaName.Text = uBoxSource.StoreAreaName;
            }
        }

        /// <summary>
        /// 仓库区域当前温度
        /// </summary>
        public decimal Temperature
        {
            get { return uBoxSource.Temperature; }
            set
            {
                uBoxSource.Temperature = value;
                lblTemperature.Text = uBoxSource.Temperature.ToString();
                ucTemperature1.Value = uBoxSource.Temperature;
            }
        }

        /// <summary>
        /// 仓库区域最小温度
        /// </summary>
        public decimal MinTemperature
        {
            get { return uBoxSource.MinTemperature; }
            set
            {
                uBoxSource.MinTemperature = value;
                ucTemperature1.BMinValue = uBoxSource.MinTemperature;
            }
        }

        /// <summary>
        /// 仓库区域最大温度
        /// </summary>
        public decimal MaxTemperature
        {
            get { return uBoxSource.MaxTemperature; }
            set
            {
                uBoxSource.MaxTemperature = value;
                ucTemperature1.BMaxValue = uBoxSource.MaxTemperature;
            }
        }

        /// <summary>
        /// 仓库区域温度范围
        /// </summary>
        public string TemperatureRange
        {
            get { return uBoxSource.TemperatureRange; }
            set
            {
                uBoxSource.TemperatureRange = value;
                lblTemperatureRange.Text = uBoxSource.TemperatureRange.ToString() + "℃";
            }
        }

        /// <summary>
        /// 温度状态（0正常，1高预警，2低预警）
        /// </summary>
        public int TemperatureState
        {
            get { return uBoxSource.TemperatureState; }
            set
            {
                uBoxSource.TemperatureState = value;
                lblTemperatureState.Text = value == 0 ? "正常" : (value == 1 ? "高预警" : "低预警");
            }
        }

        /// <summary>
        /// 指示灯颜色
        /// </summary>
        public Color StateColor
        {
            get { return uBoxSource.StateColor; }
            set
            {
                uBoxSource.StateColor = value;
                ucStatus1.ForeColor = uBoxSource.StateColor;
                lblTemperatureState.ForeColor = uBoxSource.StateColor;

            }
        }

        /// <summary>
        /// 设置按钮的显示（升温或降温按钮）
        /// </summary>
        public bool BtnSetVisible
        {
            get { return uBoxSource.BtnSetVisible; }
            set
            {
                uBoxSource.BtnSetVisible = value;
                btnSetTemperature.Visible = uBoxSource.BtnSetVisible;
            }
        }

        /// <summary>
        /// 设置按钮的文本
        /// </summary>
        public string BtnSetText
        {
            get { return uBoxSource.BtnSetText; }
            set
            {
                uBoxSource.BtnSetText = value;
                btnSetTemperature.Text = uBoxSource.BtnSetText;
            }
        }


        /// <summary>
        /// 产品数量
        /// </summary>
        public int ProductCount
        {
            get { return uBoxSource.ProductCount; }
            set
            {
                uBoxSource.ProductCount = value;
                lblProductCount.Text = uBoxSource.ProductCount.ToString();
            }
        }

        public event Action<object, EventArgs> SetTemperature;//设置分区温度事件

        /// <summary>
        /// 升温或降温
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnSetTemperature_Click(object sender, EventArgs e)
        {
            UCStoreAreaArgs args = new UCStoreAreaArgs(StoreAreaId, StoreAreaNo, Temperature, TemperatureState, MinTemperature, MaxTemperature);
            SetTemperature?.Invoke(sender, args);
        }
    }
}
